// miniprogram/pages/index/index.js
const api = require("../../utils/api.js");
const app = getApp().globalData;
Page({ 
  /**
   * 页面的初始数据
   */
  data:{
    indexData:{},
    cuorseName:"",
    carousel:{},
    navSwiper:{},
    shadeShow:false,
    width:0,
    height:0
  },
  //获取设备信息
  getAdviceInfo:(that)=>{
    let windowAll = wx.getStorageSync("WINDOWALL");
    let res = wx.removeStorageSync('WINDOWALL');
    if(windowAll){
      that.setData({
        width:windowAll.width,
        height:windowAll.height
      });
    }
  },
  //跳转到选择课程界面
  switchSubject:function(){
    wx.navigateTo({
      url: "../../group_admin/pages/switch_subject/switch_subject"
    });
  },
  //临时登陆
  indexTryUsing:async function(that){
    var parmas = {
      from:app.from
    };
    var url="/userInfo/tryUsing";
    let userData = await api.sendPostsSync(url,parmas);
    if(userData){
      wx.setStorageSync('USERDATA',userData);
    }else{
      console.log('临时登陆数据获取失败');
    }
    return userData;
  },
  //显示遮罩
  showShade:(that)=>{
    if(!wx.getStorageSync("loginFirst")){
      //隐藏底部导航栏
      wx.hideTabBar({
        animation:false
      });
      //显示遮罩
      that.setData({
        shadeShow:true
      });
      wx.setStorageSync("loginFirst",true);
    }else{
      wx.setStorageSync("loginFirst",true);
    }
  },
  //关闭遮罩
  indexShade:function(){
    //关闭遮罩
      this.setData({
        shadeShow:false
      });
    //显示tabbar
    wx.showTabBar();
  },
  //跳转内置页面
  getModuleListItem:function(res){
    console.log(res);
    var module = res.currentTarget.dataset.itemlist.module;
    //将module写入缓存
    var moduleList = {
      module:res.currentTarget.dataset.itemlist.module
    };
    wx.setStorageSync('MODULELIST', moduleList);
    let url="../../group_admin/pages/";
    switch(module){
      //考点练习
      case '17':
        url=url+"examPoint/examPoint";
        break;
      //智能刷题
      case '31':
        url=url+"int_do_prob/int_do_prob";
        break;
      //模拟测试
      case '5':
        url=url+"demoCeShi/demoCeSHi";
        break;
      //错题回顾
      case '7':
        url=url+"faileSave/faileSave?isTrue=0";
        break;
      //每日一练
      case '133':
        url=url+"dailyPractice/dailyPractice";
        break;
      //高频题库
      case '20':
        url=url+"highFrequency/highFrequency";
        break;
      //我的收藏
      case '12':
        url=url+"faileSave/faileSave?isTrue=1";
        break;
      //考前押题11
      case '293':
        url=url+"perExam/preExam";
        break;
      //考前押题22
      case '6':
        url=url+"perExam/preExam";
        break;
      //历年真题
      case '11':
        url=url+"past_exam_papers/past_exam_papers";
        break;
    }

    wx.navigateTo({
      url: url
    });
  },
  //对幻灯片相关属性赋值
  indexCarousel:function(that,adList){
    var carousel = {
      topBanner:0,
      color:"#FFFFFF",
      activeColor:"#FFFFFF",
      autoplay:true
    };
    if(adList){
      adList.forEach(function(item,index){
        if(item.position=="topBanner"){
          carousel.topBanner++;
        }
      });
    }
    that.setData({
      carousel:carousel
    });
  },
  //对导航栏的初始数据进行封装
  indexNav:function(that,moduleList){
    //每页展示条数
    const navNum=8;
    if(moduleList){
      //展示页数
      const navPage = Math.ceil(moduleList.length/navNum);
      //导航栏数据
      var nav=[];
      for(var i=0;i<navPage;i++){//控制页面数
         nav[i]=[];
        for(var j=0;j<navNum;j++){//控制住每个页面只显示8个
          nav[i][j]=moduleList[i*navNum+j];
        }
      }
    }
    //swiper数据封装
    var navSwiper={
      activeColor:"#FECC34",
      color:"#ECECEC",
      length:nav.length
    };
    that.setData({
      nav:nav,
      navSwiper:navSwiper
    });
  },
  //获取首页展示数据
  indexData:async function(that){
    var userData = wx.getStorageSync('USERDATA');
    var courseInfo = wx.getStorageSync('COURSEINFO');
    var categoryInfo = wx.getStorageSync('CATEGORYINFO');
    var url='/index/getIndexData';
    var params = {
      sessionid:userData.sessionid,
      uid:userData.uid,
      market:app.market,
      courseId:courseInfo.id,
      categoryId:categoryInfo.id
    };
    let res = await api.sendPostsSync(url,params);
    that.setData({
      indexData:res
    });
    return res;
  },
  //设置页面标题样式
  indexTitle:function(that){
    var categoryInfo = wx.getStorageSync('CATEGORYINFO');
    wx.setNavigationBarTitle({
      title: categoryInfo.shorttitle
    })
  },
   //初始化考试信息
  indexCategory:function(that){
    var courseInfo = wx.getStorageSync('COURSEINFO');
    that.setData({
      cuorseName:courseInfo.title
    });
  },
  //初始化热门试卷
  indexExam:async function(that) {
    var userData = wx.getStorageSync('USERDATA');
    var courseInfo = wx.getStorageSync('COURSEINFO');
    var parmas={
      uid:userData.uid,
      sessionid:userData.sessionid,
      market:app.market,
      courseid:courseInfo.id,
      type:7,
      isPageing:0
    }
    var url="/study/getUnitlist";
    api.sendPostsSync(url,parmas)
    .then(
      value=>{
        that.setData({
          unitlist:value.unitlist
        });
      },
      reason=>{
        console.log('热门试卷数据获取失败',reason);
      }
    );
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad:function (options) {  
    let that = this;
    //获取设备信息
    that.getAdviceInfo(that);
    //遮罩的显示
    that.showShade(that);
    //判断是否需要临时登陆
    if(!wx.getStorageSync('USERDATA')){
      //临时登陆,接收临时登陆返回信息
      that.indexTryUsing(that)
      .then(value=>that.indexData(that))
      .then(
        value=>{
          //调用幻灯片属性初始化函数
          that.indexCarousel(that,value.adList);
          //初始化导航栏数据
          that.indexNav(that,value.moduleList);
          //初始化热门试卷
          that.indexExam(that);
        },
        reason=>{
          console.log('幻灯片属性初始化函数--初始化导航栏数据--失败',reason);
        }
      )
    }else{
      //初始化首页数据
      that.indexData(that)
      .then(
        value=>{
          //调用幻灯片属性初始化函数
          that.indexCarousel(that,value.adList);
          //初始化导航栏数据
          that.indexNav(that,value.moduleList);
          //初始化热门试卷
          that.indexExam(that);
        },
        reason=>{
          console.log('幻灯片属性初始化函数--初始化导航栏数据--失败',reason);
        }
      )
    }
    //设置页面标题样式
    that.indexTitle(that);
    //初始化考试信息
    that.indexCategory(that);
  }
  ,

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})